package thread.assignment.url;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.concurrent.TimeUnit;

public class HttpURLConnection_Test {

	public static void main(String[] args) {
		final String dir = System.getProperty("user.dir");
		
		URL url = null;

		try {
			url = new File(dir + "/bin/thread/assignment/url/table.html").toURI().toURL();
			
			long startTime = System.nanoTime();
			
			for (int i = 1; i <= 10; i++) {
				int lines = readHTML(url);
				
				//System.out.println("Number of lines read : " + lines);
			}
			
			long elapsedTime = System.nanoTime() - startTime;
			System.out.println("Time taken : " + TimeUnit.SECONDS.convert(elapsedTime, TimeUnit.NANOSECONDS) + " Seconds");

		} catch (MalformedURLException ex) {
			ex.printStackTrace();
		}

	}

	public static int readHTML(URL url) {
		
		int totalLines = 0;
		
		try (BufferedReader reader = new BufferedReader(new InputStreamReader(
				url.openStream()))) {
			
			String line;
			while ((line = reader.readLine()) != null) {
				totalLines += line.length();
			}
			
		} catch (IOException e) {
			e.printStackTrace();
		}
		return totalLines;
	}
}
